package com.xtayfjpk.zookeeper.test.master_workers.callback.client;

import org.apache.log4j.Logger;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.Code;

import com.xtayfjpk.zookeeper.test.master_workers.Client;
import com.xtayfjpk.zookeeper.test.master_workers.TaskObject;
import com.xtayfjpk.zookeeper.test.master_workers.callback.BaseStringCallback;

public class CreateTaskCallback extends BaseStringCallback {
	private static final Logger LOGGER = Logger.getLogger(CreateTaskCallback.class);
	private Client client;
	public CreateTaskCallback(Client client) {
		this.client = client;
	}
	
	@Override
	protected void connectionLossCaseAction(int rc, String path, Object ctx, String name) {
		client.sumbitTask((TaskObject) ctx);
	}

	@Override
	protected void okCaseAction(int rc, String path, Object ctx, String name) {
		LOGGER.info("My created task name: " + name);
        ((TaskObject) ctx).setTaskName(name);
        client.watchTaskStatus(name.replace("/tasks/", "/status/"), ctx);
	}
	
	@Override
	protected void defaultCaseAction(int rc, String path, Object ctx, String name) {
		LOGGER.error("Something went wrong" + KeeperException.create(Code.get(rc), path));
	}
}
